package com.adapter;


import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.delegate.LogLatenessDelegate;
import com.hronline.R;
import com.models.LogLateness;

public class LogLatenessAdapter extends BaseAdapter {

	private Context context;
	private LogLateness loglateness;
	private LayoutInflater inflater;
	private LogLatenessDelegate delegate;
	
	public LogLatenessAdapter(Context context, LogLateness loglateness, LogLatenessDelegate delegate){
		super();
		
		this.context		=	context;
		this.loglateness	=	loglateness;
		this.delegate		=	delegate;	
		
		inflater	=	(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void setLogLateness(LogLateness logLateness){
		this.loglateness	=	logLateness;
	}
	
	private String convertDateToDisplayString(Date date){
		
		SimpleDateFormat	formatter	=	new SimpleDateFormat("EEE dd MMM HH:mm");
		
		return formatter.format(date);
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(position == 0){ //holiday year section
			View view	=	inflater.inflate(R.layout.holiday_section_item, null);
			
			TextView txtYear			=	(TextView)view.findViewById(R.id.txtYearValue);
			TextView txtStartDateLabel	=	(TextView)view.findViewById(R.id.txtStartDateLabel);
			TextView txtStartDateValue	=	(TextView)view.findViewById(R.id.txtStartDateValue);
			TextView txtEndDateLabel	=	(TextView)view.findViewById(R.id.txtEndDateLabel);
			TextView txtEndDateValue	=	(TextView)view.findViewById(R.id.txtEndDateValue);
			TextView txtReasonLabel		=	(TextView)view.findViewById(R.id.txtReasonLabel);
			TextView txtReasonValue		=	(TextView)view.findViewById(R.id.txtReasonValue);
			
			txtYear.setText(this.loglateness.getHolidayYear());
			txtStartDateLabel.setText(this.context.getString(R.string.text_log_lateness_start_date));
			txtStartDateValue.setText(convertDateToDisplayString(this.loglateness.getStartDate()));
			txtEndDateLabel.setText(this.context.getString(R.string.text_log_lateness_end_date));
			txtEndDateValue.setText(convertDateToDisplayString(this.loglateness.getEndDate()));
			txtReasonLabel.setText(this.context.getString(R.string.text_log_lateness_reason));
			txtReasonValue.setText(this.loglateness.getReason());
			
			View holidayView	=	view.findViewById(R.id.holidayYearRow);
			holidayView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					delegate.didClickHolidayYearRow();
				}
			});
			
			View startDateView	=	view.findViewById(R.id.startDateRow);
			startDateView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					delegate.didClickStartDateRow();
				}
			});
			
			View endDateView	=	view.findViewById(R.id.endDateRow);
			endDateView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					delegate.didClickEndDateRow();
				}
			});
			
			View reasonView		=	view.findViewById(R.id.reasonView);
			reasonView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					delegate.didClickReasonRow();
				}
			});
			
			return view;
		}
		else{ //detail section
			View view	=	inflater.inflate(R.layout.detail_row_item, null);
			
			EditText edtDetail	=	(EditText)view.findViewById(R.id.edtDetail);
			edtDetail.setText(this.loglateness.getDetails());
			
			return view;
		}
	}

}
